package com.quanquan.management.controller;

import DTO.PageDTO;
import base.BaseConst;
import com.quanquan.management.dto.FloorDTO;
import com.quanquan.management.request.FloorRequest;
import com.quanquan.management.service.FloorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import request.PageRequestBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述:
 *
 * @ClassName: FloorController
 * @Author: xiaoqianh丶
 * @Date: 2019-04-09 18:26
 * @Version: V1.0
 */
@Controller
@RequestMapping("/floor")
public class FloorController extends ManagerBaseController{

    @Autowired
    private FloorService floorService;

    @GetMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) boolean isAsc, Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0){
            page = page -1;
        }
        PageRequestBody requestBody = new PageRequestBody(page, pageSize, keyword, sort, isAsc);
        PageDTO<FloorDTO> pageDTO = floorService.listAFloors(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "floor/list";
    }

    /**
     * 添加
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(FloorRequest requestBody, Model model) {
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            return "floor/form";
        }
        boolean add = floorService.addFloor(requestBody);
        if (add) {
            return "redirect:/floor/list";
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("code",-1);
            result.put("msg","添加失败");
            model.addAllAttributes(result);
            return null;
        }
    }

    /**
     * 编辑
     * @param id
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String id, FloorRequest requestBody, Model model) {
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            FloorDTO byId = floorService.findById(id);
            model.addAttribute("editable",byId);
            return "floor/form";
        }
        boolean update = floorService.update(requestBody);
        if (update) {
            return "redirect:/floor/list";
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("code",-1);
            result.put("msg","修改失败");
            model.addAllAttributes(result);
            return null;
        }
    }
    @GetMapping("/delete")
    public String delete(@RequestParam String id,Model model) {
        boolean delete = floorService.delete(id);
        if (delete) {
            return "redirect:/floor/list";
        } else {
            model.addAttribute("code", -1);
        }
        return null;
    }

}
